package com.express.sys.service.impl;

import com.express.entity.User;
import com.express.entity.UserRole;
import com.express.sys.mapper.UserMapper;
import com.express.sys.service.UserService;
import com.express.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service实现类
 *
 * @author zhouling
 * @since 2018年7月5日 上午11:54:24
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getById(Integer id) {
        return this.userMapper.selectByPrimaryKey(id);
    }
    @Override
    public void add(User user) {
        this.userMapper.insertSelective(user);
    }
    @Override
    public List<User> getByUsername(String username) {
        return this.userMapper.selectByUsername(username);
    }
    @Override
    public List<User> getList() {
        return this.userMapper.selectPage(new User());
    }
    @Override
    public List<User> login(User user) {
        return this.userMapper.login(user);
    }
    @Override
    public void delete(int[] idsInt) {
        this.userMapper.deleteByPrimaryKey(idsInt);
    }
    @Override
    public void updatePassword(User user) {
        this.userMapper.updatePassword(user);
    }
    @Override
    public void addURole(UserRole userRole) {
        this.userMapper.addURole(userRole);
    }
    @Override
    public void updateURole(UserRole userRole) {
        this.userMapper.updateURole(userRole);
    }
    @Override
    public void deleteURole(Integer userId) {
        this.userMapper.deleteURole(userId);
    }
    @Override
    public List<UserRole> listUserRole(Integer userId) {
        return this.userMapper.listUserRole(userId);
    }
    @Override
    public List<User> getUser() {
        return this.userMapper.getUser();
    }

    @Override
    public User addLogin(String username, String password) {
        User user = null;
        try {
            user = new User();
            user.setUsername(username);
            user.setPassword(MD5Util.md5(password));
            List<User> userList = this.userMapper.login(user);
            if(null == userList || userList.size() == 0) {
                return null;
            }
            user = userList.get(0);
            if(StringUtils.isBlank(user.getPhone())){
                System.out.println("未绑定手机号");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    @Override
    public User getUser(User user) {
        if(null == user) {
            return null;
        }
        if(StringUtils.isNotBlank(user.getPhone())){
            return userMapper.selectOne(user);
        }
        return null;
    }

    @Override
    public int insert(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int delete(String id) {
        return 0;
    }

    @Override
    public User selectById(String id) {
        return null;
    }

    @Override
    public User selectOne() {
        return null;
    }

    @Override
    public List<User> selectList() {
        return null;
    }
}
